package com.song.ssm.config;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInterceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

/**
 * @Author 宋伟宁
 * @Date 2023/9/1
 * @Version 1.0
 **/
public class MyBatisConfig {

    /**
     * 创建SqlSessionFactory
     * @param ds
     * @return
     */
    @Bean
    public SqlSessionFactoryBean getSessionFactoryBean(@Autowired DataSource ds) {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        //设置数据源
        sessionFactoryBean.setDataSource(ds);
        //设置实体类别名路径
        sessionFactoryBean.setTypeAliasesPackage("com.song.ssm.pojo");
        //设置分页插件
        sessionFactoryBean.setPlugins(new PageInterceptor());

        return sessionFactoryBean;
    }

    /**
     * 创建MapperScannerConfigurer
     * @return
     */
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.song.ssm.mapper");
        return  mapperScannerConfigurer;
    }

}
